<html><head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="revisit-after" content="7 days">
<meta name="keywords" content="C#,tutorial,Microsoft Intermediate Language, MSIL">
<meta name="description" content="C# tutorial: MSIL">
<title> Hacker's introduction to the Microsoft Intermediate Langauge (MSIL)</title>
<link rel="StyleSheet" href="exemplos-Hacker_arquivos/default.css" type="text/css">
<!--start header -->
<script type="text/javascript">
var bgColor="#E1E180";
function loaded()
{
	var sPath = window.location.pathname;
	var rPos=sPath.lastIndexOf('/');
	var newPath=sPath.substring(0,rPos);
    var parentPage = newPath.substring(newPath.lastIndexOf('/') + 1);
    var currentPage=sPath.substring(rPos+1);
    if(parentPage=="Reviews")
    {
		    
		var elem=document.getElementById("csharpReviews");
        elem.style.backgroundColor=bgColor;
    }
    else if(parentPage=="Tutorials")
    {
    	var elem=document.getElementById("csharpTut");
    	elem.style.backgroundColor=bgColor;
    }
    else if(parentPage=="XmlTutorial")
    {
    	var elem=document.getElementById("xmlTut");
    	elem.style.backgroundColor=bgColor;
    }
    else if(parentPage=="Books")
    {
    	var elem=document.getElementById("csharpBooks");
    	elem.style.backgroundColor=bgColor;
    }
    setCurrentPage(currentPage);
}
</script>
</head>

<body onload="loaded();">


<a href="http://csharpcomputing.com/index.htm"><img src="exemplos-Hacker_arquivos/logo.jpg" alt="Logo: C# Computing" height="29" width="252" border="0"></a>


<table cellpadding="5">
<tbody><tr>
<td><div id="home"><h3><a href="http://csharpcomputing.com/index.htm">Home</a></h3></div></td>
	<td><div style="background-color: rgb(225, 225, 128);" id="csharpTut"><h3><a href="http://csharpcomputing.com/Tutorials/TOC.htm">C# Tutorial</a></h3></div></td><td><div id="xmlTut"><h3><a href="http://csharpcomputing.com/XMLTutorial/TOC.htm">XML Tutorial</a></h3></div></td>
	<td><div id="csharpReviews"><h3><a href="http://csharpcomputing.com/Reviews/toc.htm">C# Review</a></h3></div></td><td><div id="csharpBooks"><h3>
<a href="http://csharpcomputing.com/Books/toc.htm">C# Books</a></h3></div>
</td>
</tr>
<tr>
<td colspan="5">
<!-- SiteSearch Google -->
<form method="get" action="http://www.google.com/custom" target="_top">
<table bgcolor="#ffffff" border="0">
<tbody><tr><td height="32" nowrap="nowrap" valign="top" align="left">

</td>
<td nowrap="nowrap">
<input name="domains" value="CsharpComputing.com" type="hidden">
<input name="q" size="16" maxlength="255" type="text">
<input name="sa" value="Google Search" type="submit">
</td></tr>
<tr>
<td>&nbsp;</td>
<td nowrap="nowrap">
<table>
<tbody><tr>
<td>
<input name="sitesearch" value="" type="radio">
<font size="-1" color="#000000">Web</font>
</td>
<td>
<input name="sitesearch" value="CsharpComputing.com" checked="checked" type="radio">
<font size="-1" color="#000000">CsharpComputing.com</font>
</td>
</tr>
</tbody></table>
<input name="client" value="pub-8843523659168955" type="hidden">
<input name="forid" value="1" type="hidden">
<input name="ie" value="ISO-8859-1" type="hidden">
<input name="oe" value="ISO-8859-1" type="hidden">
<input name="flav" value="0000" type="hidden">
<input name="sig" value="Ypq9fbx2lZHwjNaa" type="hidden">
<input name="cof" value="GALT:#008000;GL:1;DIV:#FFFFFF;VLC:663399;AH:center;BGC:FFFFFF;LBGC:FFFFFF;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;LH:29;LW:252;L:http://csharpcomputing.com/images/logo.jpg;S:http://CsharpComputing.com;FORID:1;" type="hidden">
<input name="hl" value="en" type="hidden">
</td></tr></tbody></table>
</form>
<!-- SiteSearch Google -->
	
</td></tr>
</tbody></table>



<!--end header-->
<h2>C# Tutorial Lesson 8: Hacker's introduction to  MSIL.</h2>
<p>
<script type="text/javascript"><!--
google_ad_client = "pub-8843523659168955";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_channel ="";
google_ad_type = "text_image";
//--></script>
<script type="text/javascript" src="exemplos-Hacker_arquivos/show_ads.js">
</script><ins style="display:inline-table;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:728px"><ins id="aswift_0_anchor" style="display:block;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:728px"><iframe allowtransparency="true" hspace="0" marginwidth="0" marginheight="0" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){i+='.call';setTimeout(h,0)}else if(h.match){i+='.nav';w.location.replace(h)}s.log&amp;&amp;s.log.push(i)}" vspace="0" id="aswift_0" name="aswift_0" style="left: 0pt; position: absolute; top: 0pt;" frameborder="0" height="90" scrolling="no" width="728"></iframe></ins></ins>
</p>
<script type="text/javascript" src="exemplos-Hacker_arquivos/toc_insert.js"></script><table cellspacing="5"><tbody><tr><td valign="top"><table style="" width="200" border="1"><tbody><tr><td style="border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson1.htm" id="Lesson1">1. Hello World</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson2.htm" id="Lesson2">2. Types Overview</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson3.htm" id="Lesson3">3. Static and Instance members</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson4.htm" id="Lesson4">4. Inheritance and Polymorphism</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson5.htm" id="Lesson5">5. Value types as References</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson6.htm" id="Lesson6">6. Heap and Stack</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson7.htm" id="Lesson7">7. Reference Types</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a style="background-color: rgb(225, 225, 128);" href="http://csharpcomputing.com/Tutorials/Lesson8.htm" id="Lesson8">8. MSIL introduction</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson9.htm" id="Lesson9">9. Windows Controls</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson10.htm" id="Lesson10">10. Windows Forms</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson11.htm" id="Lesson11">11. Internet Programming</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson12.htm" id="Lesson12">12. MSIL II</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson13.htm" id="Lesson13">13. GDI Programming</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson14.htm" id="Lesson14">14. Delegates</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson15.htm" id="Lesson15">15. Events</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson16.htm" id="Lesson16">16. Numerical Methods</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson17.htm" id="Lesson17">17. ADO.NET</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson18.htm" id="Lesson18">18. PInvoke</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson19.htm" id="Lesson19">19. Creating Components</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson20.htm" id="Lesson20">20. Exceptions</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson21.htm" id="Lesson21">21. Recursion</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson22.htm" id="Lesson22">22. Windows Service</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson23.htm" id="Lesson23">23. Optimization</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson24.htm" id="Lesson24">24. Documentation and maintainance</a></td></tr><tr><td style="border-top: 0px none; border-bottom: 0px none;"><a href="http://csharpcomputing.com/Tutorials/Lesson25.htm" id="Lesson25">25. Linked List</a></td></tr></tbody></table></td><td>
<table><tbody><tr><td>
<p>
MSIL is the lowest level .NET language. All languages targeting .NET 
framework generate MSIL.
As a C# developer, you will probably never write MSIL code directlly, 
but you will often look at the MSIL disassembly of your application for 
answers to dependency, versioning and optimization questions.
</p>
<p>

To start disassembling C# (or any other .NET application) you need to 
run ildasm.exe from either VS.NET command prompt or .NET framework 
command prompt. On my computer, ildasm.exe is at C:\Program 
Files\Microsoft Visual Studio 8\SDK\v2.0\Bin.
</p>
<p>
To get you excied about MSIL, let me ask you a question: Where does the
term
boxing come from? Is there such a keyword in C#? What about VB.Net? Why 
do we call value to object conversion boxing and the oposite conversion <i>unboxing</i>? Because, MSIL is using <i>box</i> and <i>unbox</i> keywords to perform these conversations.
</p>
<p>
So, let's study
MSIL. The simplest MSIL program
is the one which does not do anything and has no data:</p></td><td>
		<iframe src="exemplos-Hacker_arquivos/cm.htm" style="width: 120px; height: 240px;" marginwidth="0" marginheight="0" frameborder="0" scrolling="no"></iframe>
</td></tr></tbody></table>



<p class="sample"><font class="ekeyword">
.assembly</font> hello{}<br>

<font class="ekeyword">
.class</font> hello<br>{<br>





<font class="ekeyword">
&nbsp;&nbsp;.method static public void </font>main() il managed<br>&nbsp;&nbsp;{<br>




&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">.entrypoint</font><br>

&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword"> ret</font><br>


&nbsp;&nbsp;}<br>


}</p>
<br>
<p>MSIL is an object oriented assembly language. As such, it retains 
object-oriented constructs of the source languages, e.g private and 
public methods.
 Every MSIL application needs to have an entry point.
Any method (not just Main) can serve as the entry point as long as it is
 decorated with .entrypoint instruction. MSIL
programs are compiled with the Intermediate language compiler, ilasm.exe
 that is located in the same directory as the dissassambler. Here is a 
more complicated program that,
once again, does not do anything but has some data.</p>

<p class="sample">
<font class="ekeyword">.assembly</font> hello{}<br>


<font class="ekeyword">.class</font> hello{<br>


<font class="ekeyword">&nbsp;&nbsp;&nbsp; </font>


<font class="ekeyword">.method static public void </font>main() il managed{<br>


<font class="ekeyword">&nbsp;&nbsp;&nbsp;</font>&nbsp;&nbsp;&nbsp;&nbsp;


<font class="ekeyword">.entrypoint</font><br>


<font class="ekeyword">&nbsp;&nbsp;&nbsp;</font>&nbsp;&nbsp;&nbsp;&nbsp;


<font class="ekeyword">.locals</font>( <font class="ekeyword">string</font> V_0)<br>


<font class="ekeyword">&nbsp;&nbsp;&nbsp;</font> &nbsp;&nbsp;&nbsp;&nbsp;


<font class="ekeyword">ldstr</font> "hi there"<br>


<font class="ekeyword">&nbsp;&nbsp;&nbsp;</font>&nbsp;&nbsp;&nbsp;&nbsp;


<font class="ekeyword">stloc.0</font><br>


<font class="ekeyword">&nbsp;&nbsp;&nbsp;</font>&nbsp;&nbsp;&nbsp;&nbsp;


<font class="ekeyword">ret</font><br>


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>


}</p>
<script type="text/javascript"><!--
google_ad_client = "pub-8843523659168955";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_channel ="";
google_ad_type = "text_image";
//--></script>
<script type="text/javascript" src="exemplos-Hacker_arquivos/show_ads.js">
</script><ins style="display:inline-table;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:728px"><ins id="aswift_1_anchor" style="display:block;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:728px"><iframe allowtransparency="true" hspace="0" marginwidth="0" marginheight="0" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){i+='.call';setTimeout(h,0)}else if(h.match){i+='.nav';w.location.replace(h)}s.log&amp;&amp;s.log.push(i)}" vspace="0" id="aswift_1" name="aswift_1" style="left: 0pt; position: absolute; top: 0pt;" frameborder="0" height="90" scrolling="no" width="728"></iframe></ins></ins>

<p>This program  has a statement .local(string V_0),which declares a 
single
local variable of type string. This declaration allows the compiler to 
allocate
"hi there" on the local stack. Because of that, stloc.0 can find "hi 
there" and pop it from the stack. Since you are working in a
managed environment, you cannot leave data in memory before quitting the
program. Memory leaks are not allowed; so every single variable which 
you have
allocated in memory has to be popped from the registers. Every program
also needs to start with a declaration of the assembly it belongs to. In
 our
case, we choose the assembly name to be the same as the class name. 
</p>
<p>Intermediate language compiler is very forgiven, and you may easily 
crash an MSIL application by inserting some invalid instructions into 
the code. For example, try adding <i>ldstr "hi there";</i> after <i>ret</i> instruction above.</p>
<p>Let's take a
look at a
bit more complicated example which still doesn't do anything useful.</p>

<p class="sample">
<font class="ecomment">//allocating and deallocating multiple variables on the stack</font><br>


<font class="ekeyword">.assembly</font> hello{}<br>


<font class="ekeyword">.class</font> hello<br>&nbsp;{<br>


&nbsp;<font class="ekeyword">.method static public void</font> main() il managed<br>&nbsp;&nbsp;{<br>


&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">.maxstack</font> 2<br>


&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">.entrypoint</font><br>


&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">.locals</font>( <font class="ekeyword">string </font>V_0, <font class="ekeyword">string </font>V_1) <font class="ecomment">//we have two local variables now</font> <br>


&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">ldstr</font> "hi there"<font class="ecomment"> //push this string on stack</font> <br>


&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">ldstr</font> "bye here" <font class="ecomment">//push second string on stack</font> <br>


&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">stloc.0</font> <font class="ecomment">//pop first string&nbsp; from the stack and store it in&nbsp; the
local variable 0.</font> <br>


&nbsp;&nbsp;&nbsp;&nbsp;<font class="ecomment">//you do not need to worry about deallocating local variables - it is done by
the runtime.</font> <br>


&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">stloc.0</font> <font class="ecomment">//pop the second string from the stack and store it in the same local
variable ("hi there" is overwritten)</font> <br>


&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">ret</font><br>


&nbsp;&nbsp;}<br>


}


</p><p>There is a new element in this program: <i>.maxstack</i> declaration. We use .maxstack
to declare the maximum number of variables we plan to have on the stack at any given
time. The default value is 1, so we can always omit this declaration when we
use a single register.</p>


<p>Here is a hello world program written in MSIL</p>

<p class="sample">
<font class="ecomment">//compile with ilasm</font><br>
<font class="ekeyword">.assembly</font> hello {}<br>
<font class="ekeyword">.method static public void </font>main() il managed <br>{<br>
&nbsp;&nbsp;<font class="ekeyword">.entrypoint</font><br>
	&nbsp;&nbsp;<font class="ekeyword">ldstr</font>	"Hello MS IL!"<br>
	&nbsp;&nbsp;<font class="ekeyword">call	void </font>[mscorlib]System.Console::WriteLine(<font class="ekeyword">class</font> 
System.String)
<br>
&nbsp;&nbsp;<font class="ekeyword">ret</font><br>
}<br>


</p><p>All MSIL directives start with a period. Any MSIL component 
(except module) is an assembly. Ilasm allows classles assemblies (see 
code above). However, classless assemblies are not compatible with 
assemblies generated from higher level .NET languages (e.g. C# and 
VB.NET).,</p>


<p>.entrypoint and ret are equivalent to main(){&nbsp;... }</p>


<p>.lsdtr loads
string into a register and calls to WriteLine picks it up from there. WriteLine
does all the clean up before it displays "hello&nbsp; msil", we do not
need to pop anything from the stack. We will get a runtime error if we do.</p>


<p>Here is a program which illustrates how to store data into local variables and
how to overwrite them</p>

<p class="sample">

<font class="ekeyword">.assembly </font>hello{}<br>
<font class="ekeyword">.assembly</font> extern mscorlib {}<br>
<font class="ekeyword">.class </font>hello<br>&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">.method static public void </font>main() il managed{<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">.maxstack </font>2<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">.entrypoint</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">.locals</font>(<font class="ekeyword">string</font> V_0, <font class="ekeyword">string </font>V_1)<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font class="ecomment">//we have two local variables now</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">ldstr </font>"hi there" <font class="ecomment">//push this string on stack</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">ldstr</font> "bye here" <font class="ecomment">//push second string on stack</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">stloc.0 </font><font class="ecomment">//pop first string  from the stack and store it in  the local variable 0.</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;<font class="ecomment">//you do not need to worry about dealocating local variables - it is done by the runtime.</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">stloc.0 </font><font class="ecomment">//pop the second string from the stack and store it in the same local variable ("bye there" is overwritten)</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">ldloc.0 <font class="ecomment">//push the remaining local variable containing "bye there" into the register</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">call void </font>[mscorlib]System.Console::WriteLine(<font class="ekeyword">string</font>)<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">ret</font><br>
&nbsp;&nbsp;}<br>
}


</font></p><p><font class="ekeyword">It is always a lot of fun to manipulate integers with Assembly language.</font></p><font class="ekeyword">


<p>&nbsp;</p>

</font><p class="sample"><font class="ekeyword">

<font class="ecomment">//print number 2</font><br>


<font class="ekeyword">.assembly</font> hello {}<br>


<font class="ekeyword">.method public static void </font> Main() il managed<br>
{<br>
&nbsp;&nbsp;<font class="ekeyword">.entrypoint</font><br>


&nbsp;&nbsp;<font class="ekeyword">.locals</font>(int32</font> V_0)<br>
&nbsp;&nbsp;<font class="ekeyword">ldc.i4</font>.2<br>
&nbsp;&nbsp;<font class="ekeyword">stloc.0</font><br>
&nbsp;&nbsp;<font class="ekeyword">ldloc.0</font><br>
&nbsp;&nbsp;<font class="ekeyword">call       void</font> [mscorlib]System.Console::WriteLine(<font class="ekeyword">int32</font>)<br>
&nbsp;&nbsp;<font class="ekeyword">ret</font><br>
}


</p><p>The next program adds two integers</p>

<p class="sample">


<font class="ecomment">//add two numbers 1 and 3</font><br>
<font class="ekeyword">.assembly</font> hello {}<br>
<font class="ekeyword">.assembly extern</font> mscorlib {}<br>
<font class="ekeyword">.class public</font> hello&nbsp;<br>
{<br>
<font class="ekeyword">&nbsp;&nbsp;&nbsp; </font>
<font class="ekeyword">.method static public void </font>main()<br>&nbsp;&nbsp; {<br>
	&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">.entrypoint</font><br>
	&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font>
	&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">.maxstack </font>2<br>
	&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">.locals</font>(<font class="ekeyword">int32 </font>V_0, &nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">int32 </font>V_1) <font class="ecomment">//declare two local variables</font><br>
	&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">ldc.i4.</font>1<font class="ecomment"> //put number&nbsp; 1 on the stack</font><br>
	&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">ldc.i4.</font>3 <font class="ecomment">//put number 3 on the stack</font><br>
	&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">stloc.0</font> <font class="ecomment">//pop 1 from the stack and store it in the local variable</font><br>
	&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">ldloc.0 </font><font class="ecomment">//push local variable&nbsp; with value 1 on the stack</font><br>
	&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">add</font> <font class="ecomment">//add takes care of the second value on the local stack</font><br>
	&nbsp;&nbsp;&nbsp;&nbsp;<font class="ecomment">//you should not try to deallocoate memory there. it is done by add</font><br>
	&nbsp;&nbsp;&nbsp;&nbsp;<font class="ecomment">//add works with the first variable on the stack
and the value</font><br>
	
	&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">call	void </font>[mscorlib]System.Console::WriteLine(<font class="ekeyword">int32</font>)<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font class="ekeyword">ret</font><br>
&nbsp;&nbsp; }<br>
}

<br>
</p><p><script type="text/javascript"><!--
google_ad_client = "pub-8843523659168955";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_channel ="";
google_ad_type = "text_image";
//--></script>
<script type="text/javascript" src="exemplos-Hacker_arquivos/show_ads.js">
</script><ins style="display:inline-table;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:728px"><ins id="aswift_2_anchor" style="display:block;border:none;height:90px;margin:0;padding:0;position:relative;visibility:visible;width:728px"><iframe allowtransparency="true" hspace="0" marginwidth="0" marginheight="0" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){i+='.call';setTimeout(h,0)}else if(h.match){i+='.nav';w.location.replace(h)}s.log&amp;&amp;s.log.push(i)}" vspace="0" id="aswift_2" name="aswift_2" style="left: 0pt; position: absolute; top: 0pt;" frameborder="0" height="90" scrolling="no" width="728"></iframe></ins></ins>
</p>
<p>
It is sometimes very useful to have an explicit conversion between a value
and an object. This is done with box directive. The example bellow outputs an
object value. So, we need to explicitly convert the data inside the register
to a boxed data.</p>

<p class="sample">
<font class="ekeyword">.assembly </font>hello{}<br>
<font class="ekeyword">.method public static void  </font>Main() il managed<br>
{<br>
  &nbsp;&nbsp;<font class="ekeyword"> .entrypoint</font><br>
 &nbsp;&nbsp;<font class="ekeyword">ldc.i4.s</font> 100 <font class="ecomment">//put 100 on stack</font><br>   
 &nbsp;&nbsp;<font class="ekeyword">box    </font>    [mscorlib]System.Int32 <font class="ecomment">//convert it to on object in place</font><br>
 &nbsp;&nbsp;<font class="ekeyword">call       void</font> [mscorlib]System.Console::WriteLine(<font class="ekeyword">object</font>) <font class="ecomment">//print the value
of the object</font><br>
 &nbsp;&nbsp;<font class="ekeyword">ret</font><br>
}


</p><p>The example above was a bit contrived to keep things simple . Here is a more realistic
example</p>


<p class="sample">
<font class="ekeyword">.assembly </font>hello{}<br>
<font class="ekeyword">.method public  static void </font> Main() il managed<br>
{<br>
&nbsp;&nbsp;<font class="ekeyword">.entrypoint</font><br>
&nbsp;&nbsp;<font class="ekeyword">.maxstack </font> 2<br>
&nbsp;&nbsp;<font class="ekeyword">.locals </font>(int32 V_0)<br>   
&nbsp;&nbsp;<font class="ekeyword">ldstr </font>     "Please enter your age:"<br>    
&nbsp;&nbsp;<font class="ekeyword">call     void </font>[mscorlib]System.Console::WriteLine(<font class="ekeyword">string</font>)<br>    
&nbsp;&nbsp;<font class="ekeyword">call     string</font> [mscorlib]System.Console::ReadLine()<br>     
&nbsp;&nbsp;<font class="ekeyword">call </font>      int32 [mscorlib]System.Int32::Parse(<font class="ekeyword">string</font>)<br>
&nbsp;&nbsp;<font class="ekeyword">stloc.0</font><br>   
&nbsp;&nbsp;<font class="ekeyword">ldstr </font>     "You are {0} years old "<br>
&nbsp;&nbsp;<font class="ekeyword">ldloc.0</font><br>  
&nbsp;&nbsp;<font class="ekeyword">box    </font>    [mscorlib]System.Int32 <font class="ecomment">//convert int32 to an object on the stack</font><br>
&nbsp;&nbsp;<font class="ekeyword">call     void </font>[mscorlib]System.Console::WriteLine(<font class="ekeyword">string</font>,<font class="ekeyword"> object</font>)<br>
&nbsp;&nbsp;<font class="ekeyword">ret</font><br>
}<br>

</p><p>
Note that MSIL does not have System.Consol::WriteLine(sting,int32 ) method, therefore
int32 needs to be converted to another type to allow output to the console.</p>


<p>Exercises:</p>


<ol>
  <li>Write a program that subtracts two integers.</li>
  <li>Read an <a href="http://msdn.microsoft.com/msdnmag/issues/01/05/bugslayer/print.asp">MSIL article</a> by John Robbins at
    MSDN magazine.</li>
  <li>Use ildasm to disassemble your .Net programs. Does compiling with /o+
    optimization option change MSIL code? Why?</li>
</ol>


<p>
<script type="text/javascript"><!--
google_ad_client = "pub-8843523659168955";
google_ad_width = 728;
google_ad_height = 15;
google_ad_format = "728x15_0ads_al";
google_ad_channel ="";
//--></script>
<script type="text/javascript" src="exemplos-Hacker_arquivos/show_ads.js">
</script><ins style="display:inline-table;border:none;height:15px;margin:0;padding:0;position:relative;visibility:visible;width:728px"><ins id="aswift_3_anchor" style="display:block;border:none;height:15px;margin:0;padding:0;position:relative;visibility:visible;width:728px"><iframe allowtransparency="true" hspace="0" marginwidth="0" marginheight="0" onload="var i=this.id,s=window.google_iframe_oncopy,H=s&amp;&amp;s.handlers,h=H&amp;&amp;H[i],w=this.contentWindow,d;try{d=w.document}catch(e){}if(h&amp;&amp;d&amp;&amp;(!d.body||!d.body.firstChild)){if(h.call){i+='.call';setTimeout(h,0)}else if(h.match){i+='.nav';w.location.replace(h)}s.log&amp;&amp;s.log.push(i)}" vspace="0" id="aswift_3" name="aswift_3" style="left: 0pt; position: absolute; top: 0pt;" frameborder="0" height="15" scrolling="no" width="728"></iframe></ins></ins>
</p>
<script type="text/javascript" src="exemplos-Hacker_arquivos/footer.js"></script></td></tr></tbody></table><h5><a href="http://csharpcomputing.com/copyright.htm">Copyright</a>© 2000-2011 <a href="http://csharpcomputing.com/About.htm">C# Computing, LLC</a> &nbsp;<a href="http://nuscho.com/Jobs">C# Jobs </a>&nbsp;&nbsp;<a href="mailto:?subject=Hacker%27s%20introduction%20to%20the%20Microsoft%20Intermediate%20Langauge%20%28MSIL%29&amp;body=Thought%20you%20might%20like%20this:%20http://CsharpComputing.com/Tutorials/Lesson8.htm">Email to a friend</a>&nbsp;&nbsp;<span style="text-decoration:underline;color:#0000ff" onclick="window.external.AddFavorite(location.href,document.title);">Bookmark</span></h5></body></html>